package com.miaowang.salad.jobqueue;

import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.Common;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.net.OkHttpUtils;
import com.miaowang.salad.net.UrlUtils;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;

import de.greenrobot.event.EventBus;

/**
 * 获取验证码
 * @author weihe
 * 2015.09.03
 */
@SuppressWarnings("serial")
public class GetIdentifyCodeJob extends Job {
	
	private String phoneNum;

	public GetIdentifyCodeJob(String phoneNum) {
		super(new Params(JobQueuePriority.PRIORITY_HIGH).groupBy(JobQueuePriority.JOBS_GROUP_USER).requireNetwork());
		this.phoneNum = phoneNum;
	}

	@Override
	public void onAdded() {
 
	}

	@Override
	protected void onCancel() {

	}

	@Override
	public void onRun() throws Throwable {
//		String url = UrlUtils.TEST+"?";
//		String result = OkHttpUtils.get(url);
			
			String url = UrlUtils.SEND_PHONE_IDENTIFY_CODE+ "?";
			JSONObject json = new JSONObject();
			json.put("phone", phoneNum);
			String result = OkHttpUtils.post(url, json.toString());
			OkHttpResponseData responseData = JSON.parseObject(result, OkHttpResponseData.class);
			EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.SEND_PHONE_IDENTIFY_CODE,responseData));
	}

	@Override
	protected boolean shouldReRunOnThrowable(Throwable throwable) {
		EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.SEND_PHONE_IDENTIFY_CODE));
		return false;
	}

}
